package com.feeling.oafeeling.service.impl;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.feeling.oafeeling.common.pojo.RedisKeyPrefix;
import com.feeling.oafeeling.mapper.SysInstitutionMapper;
import com.feeling.oafeeling.pojo.SysInstitution;
import com.feeling.oafeeling.pojo.SysInstitutionNode;
import com.feeling.oafeeling.service.SysInstitutionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
@Slf4j
public class SysInstitutionServiceImpl implements SysInstitutionService {
    @Autowired
    private SysInstitutionMapper sysInstitutionMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<SysInstitutionNode> getInstitutionTree(SysInstitution institution) {
        List<SysInstitutionNode> rootNodes = new ArrayList<>();
//        Object o = redisTemplate.opsForValue().get(RedisKeyPrefix.SYSINSTITUTION_KEY);
//        if (o != null && !"".equals(o.toString())) {
//            return JSONObject.parseArray(o.toString(), SysInstitutionNode.class);
//        }
        List<SysInstitution> sysInstitutions = sysInstitutionMapper.selectAll(institution);
        for (SysInstitution sysInstitution : sysInstitutions) {
            if ("/".equals(sysInstitution.getParentInstitutionCode()) &&
                    "0".equals(sysInstitution.getInstitutionRank())) {
                SysInstitutionNode rootNode = new SysInstitutionNode();
                rootNode.setKey(sysInstitution.getInstitutionCode());
                rootNode.setInstitutionName(sysInstitution.getInstitutionName());
                rootNode.setStatus(sysInstitution.getStatus().toString());
                rootNode.setInstitutionRank(sysInstitution.getInstitutionRank().toString());
                rootNode.setChildren(new ArrayList<>());
                rootNodes.add(rootNode);
            }
        }
        buildInstitutionTree(rootNodes, sysInstitutions);
        String s = JSON.toJSONString(rootNodes);
        redisTemplate.opsForValue().set(RedisKeyPrefix.SYSINSTITUTION_KEY,s);
        return rootNodes;
    }

    @Override
    public void buildInstitutionTree(List<SysInstitutionNode> nodes, List<SysInstitution> sysInstitutions) {
        for (SysInstitutionNode parentNode : nodes) {
            List<SysInstitutionNode> children = new ArrayList<>();
            for (SysInstitution sysInstitution : sysInstitutions) {
                if (sysInstitution.getParentInstitutionCode() != null
                        && sysInstitution.getParentInstitutionCode().equals(parentNode.getKey())) {
                    SysInstitutionNode childNode = new SysInstitutionNode();
                    childNode.setKey(sysInstitution.getInstitutionCode());
                    childNode.setInstitutionName(sysInstitution.getInstitutionName());
                    childNode.setStatus(sysInstitution.getStatus().toString());
                    childNode.setInstitutionRank(sysInstitution.getInstitutionRank().toString());
                    childNode.setChildren(new ArrayList<>());
                    children.add(childNode);
                }
            }
            parentNode.setChildren(children);
            buildInstitutionTree(children, sysInstitutions);
        }
    }

    @Override
    public void upDataInstitution(SysInstitution sysInstitution) {
        try {
            sysInstitutionMapper.upDataInstitution(sysInstitution);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<SysInstitution> getInstitutionByPage(Integer currentPage, Integer pageSize) {
        return sysInstitutionMapper.getInstitutionByPage((currentPage-1) * pageSize, pageSize);
    }

    @Override
    public void addInstitutionTree(SysInstitution sysInstitution) {
        SysInstitution sysInstitution1 = sysInstitutionMapper.selectOne(sysInstitution);
        if (sysInstitution1 != null) {
            sysInstitution.setInstitutionRank((Integer.parseInt(sysInstitution1.getInstitutionRank()) + 1) + "");
            sysInstitution.setParentInstitutionName(sysInstitution1.getParentInstitutionName());
        }
        sysInstitutionMapper.addInstitutionTree(sysInstitution);
    }
}
